package com.example.myanimator.scroll

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import android.view.animation.AnimationUtils
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.RelativeLayout
import android.widget.Toast
import androidx.core.view.setMargins
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.viewpager2.widget.ViewPager2
import com.bumptech.glide.Glide
import com.example.myanimator.R
import com.example.myanimator.circleanimator.CircleAnimatorView
import com.example.myanimator.databinding.ActivityScrollerBinding

/**
 * @author tgw
 * @date 2021/11/22
 * @describe 滑动布局的activity
 */
class ScrollActivity : AppCompatActivity() {


    private lateinit var mBind: ActivityScrollerBinding
    private lateinit var imageViewList: ArrayList<ImageView>
    private var imageViewListData = ArrayList<String>()

    private var currentPosition2 = 1


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mBind = DataBindingUtil.setContentView(this, R.layout.activity_scroller)
        initViewPagerData()
    }

    private fun initViewPagerData() {
        prepareData()
        initViewpager()
        initViewpager2()
        initViewpager3()
        initViewpager4()
        initViewpager5()

        mBind.llRcParent.setOnTouchListener(object : View.OnTouchListener{
            override fun onTouch(v: View?, event: MotionEvent?): Boolean {

                return true
            }

        })
        mBind.llIntercept.setOnClickListener(){
            Log.d("tgw拦截", "onClick: ")
            Toast.makeText(this,"fasfa",Toast.LENGTH_SHORT).show()
        }
    }

    override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
        return super.dispatchTouchEvent(ev)
    }

    override fun onTouchEvent(event: MotionEvent?): Boolean {
        Toast.makeText(this,"MotionEvent",Toast.LENGTH_SHORT).show()
        return super.onTouchEvent(event)
    }

    private fun initViewpager() {
        mBind.viewpager.setClipToPadding(false);
// set padding manually, the more you set the padding the more you see of prev & next page
        mBind.viewpager.setPadding(40, 0, 40, 0);
// sets a margin b/w individual pages to ensure that there is a gap b/w them
        mBind.viewpager.setPageMargin(20);
        mBind.viewpager.adapter = ViewPagerImageAdapter(imageViewList, mBind.viewpager, this)
        mBind.viewpager.currentItem = 1

    }

    private fun initViewpager5() {
        mBind.recyclevielistadapter.layoutManager = LinearLayoutManager(this).apply {
            orientation = LinearLayoutManager.HORIZONTAL
        }
        val listAdapter =  RecycleViewImageListAdapter(this)
        mBind.recyclevielistadapter.adapter = listAdapter
        mBind.recyclevielistadapter.scrollToPosition(100)
//        mBind.recyclevielistadapter.start()
//        prepareDataListadapter()
        listAdapter.submitList(imageViewListData)
        mBind.btChange.setOnClickListener(){
            mBind.recyclevielistadapter.scrollToPosition(0)
            listAdapter.submitList(prepareDataListadapter())
        }
    }

    private fun initViewpager4() {
        mBind.recycleview.layoutManager = LinearLayoutManager(this).apply {
            orientation = LinearLayoutManager.HORIZONTAL
        }
        mBind.recycleview.adapter = RecycleViewImageAdapter(imageViewListData, this)
        mBind.recycleview.start()
    }

    private fun initViewpager3() {
        Glide.with(this).load(imageViewListData[0]).into(mBind.img1)
        Glide.with(this).load(imageViewListData[1]).into(mBind.img2)
        mBind.viewflipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.right_in))
        mBind.viewflipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.left_out))
        mBind.viewflipper.flipInterval = 3000

//        mBind.viewflipper.startFlipping()
        mBind.viewflipper.showNext();
        mBind.viewflipper.showNext();
    }

    private fun initViewpager2() {
//        val params: ViewGroup.LayoutParams = ViewGroup.LayoutParams(
//            (1080 * 0.84).toInt(),
//            (1920 - CircleAnimatorView.dip2px(this, 70f)).toInt()
//        )
//
//        mBind.viewpager2.setClipToPadding(false);
//// set padding manually, the more you set the padding the more you see of prev & next page
//        mBind.viewpager2.setPadding(40, 0, 40, 0);
//// sets a margin b/w individual pages to ensure that there is a gap b/w them
//        mBind.viewpager2.setPadding(20);
//
////        params.addRule(RelativeLayout.CENTER_IN_PARENT)
//        params.setMargins(
//            (1080 * 0.08).toInt(),
//            CircleAnimatorView.dip2px(this, 20f).toInt(),
//            0,
//            CircleAnimatorView.dip2px(this, 20f).toInt()
//        )
//        mBind.viewpager2.setLayoutParams(params)

        mBind.viewpager2.adapter = ViewPager2ImageAdapter(imageViewListData, this)
        mBind.viewpager2.setPageTransformer(DepthPageTransformer())
        mBind.viewpager2.currentItem = currentPosition2
        mBind.viewpager2.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {
                Log.d(
                    "tgw2",
                    "onPageScrolled:position:$position--positionOffset:$positionOffset--positionOffsetPixels:$positionOffsetPixels "
                )
                super.onPageScrolled(position, positionOffset, positionOffsetPixels)
            }

            override fun onPageScrollStateChanged(state: Int) {
                Log.d("tgw2", "onPageScrollStateChanged:state:$state ")
                super.onPageScrollStateChanged(state)
            }

            override fun onPageSelected(position: Int) {
                Log.d("tgw2", "onPageSelected:position: $position")
                super.onPageSelected(position)
                Log.d("tgw2", "onPageSelected: \$position:$position")
                currentPosition2 = position

                if (currentPosition2 == 0) {
                    mBind.viewpager2.setCurrentItem(imageViewList.size - 2, false) //切换，不要动画效果
                } else if (currentPosition2 == imageViewList.size - 1) {
                    mBind.viewpager2.setCurrentItem(1, false) //切换，不要动画效果
                }
            }
        })

    }

    private fun prepareData() {
        imageViewList = ArrayList<ImageView>()

        imageViewListData.add("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg%2Ftp05%2F19100220060CY5-0-lp.jpg&refer=http%3A%2F%2Fimg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1640141966&t=89c9ff11ecb77db9feb6879bc322bbab")
        imageViewListData.add(
            "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fbpic.588ku.com%2Felement_origin_min_pic%2F16%2F10%2F29%2F2ac8e99273bc079e40a8dc079ca11b1f.jpg&refer=http%3A%2F%2Fbpic.588ku.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1640141947&t=16b7c85080dcd24c4cc9793fdad0debd"
        )
        imageViewListData.add(
            "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Ffile02.16sucai.com%2Fd%2Ffile%2F2014%2F0829%2Fb871e1addf5f8e96f3b390ece2b2da0d.jpg&refer=http%3A%2F%2Ffile02.16sucai.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1640141966&t=7ee434309b805001436b3b1727faa460"
        )
        imageViewListData.add(
            "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg%2Ftp05%2F19100220060CY5-0-lp.jpg&refer=http%3A%2F%2Fimg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1640141966&t=89c9ff11ecb77db9feb6879bc322bbab"
        )
        imageViewListData.add(
            "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fbpic.588ku.com%2Felement_origin_min_pic%2F16%2F10%2F29%2F2ac8e99273bc079e40a8dc079ca11b1f.jpg&refer=http%3A%2F%2Fbpic.588ku.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1640141947&t=16b7c85080dcd24c4cc9793fdad0debd"
        )


        for (index in imageViewListData.indices) {
            var image = ImageView(this)
            Glide.with(this).load(imageViewListData[index]).into(image)
            imageViewList.add(image)
        }
    }

    private fun prepareDataListadapter(): ArrayList<String> {
        val imageViewList = ArrayList<String>()


        for (index in  imageViewListData.indices){
            imageViewList.add(imageViewListData[index])
        }
        imageViewList[0] = "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fblog%2F201403%2F29%2F20140329145413_hw4HA.thumb.1000_0.jpeg&refer=http%3A%2F%2Fc-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1640163656&t=71c85fbd047afd8437e93dfa9c7171ef"
        return imageViewList
    }


}